package com.eascs.spider.dsk.spider;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.eascs.spider.dsk.db.MongoSession;
import com.eascs.spider.dsk.db.RedisSession;
import com.eascs.spider.dsk.downloader.Downloader;
import com.eascs.spider.dsk.entity.QueryForm;
import com.eascs.spider.dsk.entity.StaticParam;

/**
 * 根据基本信息队列取条件采集单个公司的详情信息
 * @author:hzp
 * @date:2018年4月17日
*/
public class DskCompanyDetailSpider implements Runnable{
	private static Logger logger = LoggerFactory.getLogger(DskCompanyDetailSpider.class);
	
	private RedisSession redisSession;
	
	private MongoSession mongoSession;
	
	private Downloader downloader;
	
	private QueryForm qr;
	
	public DskCompanyDetailSpider() {
		this.redisSession = RedisSession.getIntance();
		this.mongoSession = MongoSession.getInstance();
		this.downloader = Downloader.build();
		this.qr = QueryForm.getInstance();
	}
	
	@Override
	public void run(){
		while(true){
			sleep();
			String value = redisSession.rpop(StaticParam.DSK_REDIS_COMPANY_QUEUE);
			if(value == null)
				continue;
			String companyId = value.split("\\|")[0];
			getDetail(companyId);
		}
	}
	
	public void getDetail(String companyId){
		String html = null;
		String phone = qr.getTel();
		try {
			String url = "http://dingmou.net:8019/dmsock/company/getCompanyDetail/"+companyId+"/"+phone;	
			downloader.setUrl(url).setHttpMethod("GET").download();
			html = downloader.getHtml();
			extractDetail(html);
		} catch (Exception e) {
			logger.error("提取公司详情报错,copanyId：{},html:{}" ,companyId ,html);
			redisSession.lpush(StaticParam.DSK_REDIS_COMPANY_QUEUE, companyId);
		}
	}	
	
	//提取单个公司详情数据
	public void extractDetail(String html){
		JSONObject json = new JSONObject(html);
		Map<String, Object> data = new HashMap<String, Object>();
		JSONObject sourceData = json.getJSONObject("data");

		Iterator<String> iterator = sourceData.keys();
		while(iterator.hasNext()){
			String key = iterator.next();
			if(!"linkInfo".equals(key)){
				data.put(key, sourceData.get(key));
				if("fCompanyID".equals(key))
					data.put("id", sourceData.get(key));
				continue;				
			}
			JSONArray linkInfo = sourceData.getJSONArray("linkInfo");
			List<Map<String, Object>> linkInfoList = new LinkedList<Map<String,Object>>();
			for(int i =0;i<linkInfo.length();i++){
				JSONObject j = linkInfo.getJSONObject(i);
				Map<String, Object> mapTemp = new HashMap<>();
				mapTemp.put("fTel", j.get("fTel"));
				mapTemp.put("fCanDel", j.get("fCanDel"));
				mapTemp.put("flinkman", j.get("flinkman"));
				mapTemp.put("LinkId", j.get("LinkId"));
				mapTemp.put("fIsFree", j.get("fIsFree"));
				linkInfoList.add(mapTemp);
			}
			data.put("linkInfo", linkInfoList);	
		}
		logger.info("单个公司数据:{}" ,JSON.toJSONString(data));
		mongoSession.set(StaticParam.DSK_DB_COLLECTION_DETAIL, data);		
	}	
	
	//随机休息20-25秒
	private void sleep(){
		try {
			int time = (int)(Math.random()*6+20);
			Thread.sleep(time*1000L);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
		
}
